from selenium import webdriver
import unittest
import time
import os
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import NoSuchElementException

class Baidu1(unittest.TestCase):
#     测试固件
    def setUp(self):
        print("-----setUp-----")
        self.driver = webdriver.Chrome()
        self.url = "https://www.baidu.com/"
        self.driver.maximize_window()
        time.sleep(3)

    def tearDown(self):
        print("-----tearDown-----")
        self.driver.quit()
#     测试用例
#     忽略测试用例的执行
#     @unittest.skip("skipping")
    def test_hao(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_link_text("hao123").click()
        time.sleep(6)

    def test_hbaidu(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        # self.assertEqual("突如其来的假期_百度搜索", driver.title, msg="实际结果和预期结果不一致" )
        self.assertTrue("百度一下，你就知道" == driver.title, msg="不一致！！！")
        driver.find_element_by_id("kw").send_keys("突如其来的假期")
        driver.find_element_by_id("su").submit()
        time.sleep(5)
        print(driver.title)
        # self.assertEqual(driver.title, "突如其来的假期_百度搜索", msg="实际结果和预期结果不一致" )
        time.sleep(6)

    def saveScreenAsPhoto(self, driver, file_name):
        if not os.path.exists("./image"):
            os.makedirs("./image")
        now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
        driver.get_screenshot_as_file("./image/" + now + "-" + file_name)
        time.sleep(3)

    if __name__ == "__main__":
        unittest.main()
